************************************

* Compute imputed residency duration

	use "${clean_data}/panel_physicians_clean", clear	

	xtset id year
	by id: gegen agemin = min(age)
	by id: gegen agemax = max(age)
	by id: gen nobs = _N
	keep if inrange(agemin,20,28) & nobs==13
	gsort id year
	* defining beginning and ending of residency based on raw and relative income
	* relative income from one year to the next
	gen relinc = (totw2comp/L1.totw2comp)*100
	gen relinc2 = (totw2comp/((totw2comp+L1.totw2comp)/2))*100
	gen resbeg = 0
	replace resbeg = 1 if inrange(totw2comp,15000,35000) & F1.relinc2>130 & ~mi(F1.relinc)
	by id: gen temp = sum(resbeg)
	replace resbeg = 0 if resbeg==1 & temp>1
	drop temp
	gen resend = relinc>130 & ptotinc>80000 & ~mi(relinc)
	by id: gen temp = sum(resend)
	replace resend = 0 if resend==1 & temp>1
	drop temp
	foreach v in resbeg resend {
		gen temp1 = year if `v'==1
		bys id: egen year_`v' = max(temp1)
		drop temp1
	}
	
	replace year_resbeg = . if year_resend < year_resbeg + 2 & !mi(year_resbeg) & !mi(year_resend)
	replace year_resend = . if year_resend < year_resbeg + 2 & !mi(year_resbeg) & !mi(year_resend)
	gen resdur = year_resend - year_resbeg
	keep id resdur year_resbeg *_code
	duplicates drop
	gisid id
	preserve 
	collapse (mean) resdur_allyrs=resdur
	save "${temp}/resdur_allyrs.dta", replace
	restore 
	preserve 
	drop if year_resbeg<2011
	collapse (mean) resdur_before2010=resdur
	save "${temp}/resdur_before2010.dta", replace
	restore 	
	foreach spec in spec_category cms_spec nrmp_spec {
	preserve 
	gegen resdur_allyrs=mean(resdur), by(`spec'_code)
	replace resdur=. if year_resbeg<2011
	gegen resdur_before2010=mean(resdur), by(`spec'_code)
	keep `spec'_code resdur_allyrs resdur_before2010
	duplicates drop
	append using "${temp}/resdur_allyrs.dta"
	append using "${temp}/resdur_before2010.dta"
	 if "`spec'" != "cms_spec"  {
	 	replace `spec'_code=0 if missing(`spec'_code) 
	 }
	 if "`spec'" == "cms_spec"  {
	 	replace  `spec'_code="00" if missing(`spec'_code) 
	 }
	replace resdur_allyrs=resdur_allyrs[_n-1] if missing(resdur_allyrs)
	replace resdur_before2010=resdur_before2010[_n+1] if missing(resdur_before2010)
	duplicates drop
	save "${temp}/resdur_`spec'.dta", replace
	restore
	}

************************************

* Compute specialty-year characteristics from analytic dataset
	
 	local specialties spec_category cms_spec nrmp_spec

	local age_group ageall age4055
	
	use "${clean_data}/panel_physicians_clean", clear	
	merge m:1 cz90 using "${intermediate_data}/cz_characteristics/CZ1990Characteristics.dta", keep(master match) keepusing(desirable_cz) nogen
	
	foreach age in `age_group' {
	
	if "`age'" == "age4055"  {
	 	keep if inrange(age, 40,55) 
	 }
	 
	foreach spec in `specialties' {

 	assert !missing(`spec'_code)

	preserve 
	gcollapse (mean) year_survey physician_nppes physician_acs ///
	      provider_enumeration_year last_npi_update_year ///
	      npi_deactivation_year npi_reactivation_year ///
	      einsize samecmsspec_share grd_yr ///
	      age age_survey female birthyr deathyr is_foreign ///
	      married spouse_in_labor_force spouse_doc ///
	      w2wgs ptotinc aginc top1 pftloss ///
	      profinc  ///	      
	      w2wgs_salary amount_tax_dividends amount_tax_interest social_security schde_profit_loss ///
	      amount_tax_exempt_inc total_money_income tax_distr_1 tax_distr_2 totw2comp ///
	      w2wgs_spouse total_compensation_spouse ///
	      schedule_c_flag schedule_e_flag schd_se ///
	      received_1099ssa retiredyear_1099ssa ///
	      retired_1099ssa retiredyear_w2wgsal retired_w2wgsal ///
	      se_nppes se_acs se_schedules govt_acs ///
	      wkh wkh_imputed desirable_cz ///
	      (sd) sd_ptotinc = ptotinc sd_logptotinc = logptotinc ///
	      (median) median_ptotinc = ptotinc median_logptotinc = logptotinc, ///
	      by(`spec'_code `spec'_desc year) labelformat(#stat#: #sourcelabel#) freq(id)	
	      tempfile clps_`spec'_`age'
	      save `clps_`spec'_`age''
	  restore    
	}
		
	preserve 

	 gcollapse (mean) year_survey physician_nppes physician_acs ///
	      provider_enumeration_year last_npi_update_year ///
	      npi_deactivation_year npi_reactivation_year ///
	      einsize samecmsspec_share grd_yr ///
	      age age_survey female birthyr deathyr is_foreign ///
	      married spouse_in_labor_force spouse_doc ///
	      w2wgs ptotinc aginc top1 pftloss ///
	      profinc ///	      
	      w2wgs_salary amount_tax_dividends amount_tax_interest social_security schde_profit_loss ///
	      amount_tax_exempt_inc total_money_income tax_distr_1 tax_distr_2 totw2comp ///
	      w2wgs_spouse total_compensation_spouse ///
	      schedule_c_flag schedule_e_flag schd_se ///
	      received_1099ssa retiredyear_1099ssa ///
	      retired_1099ssa retiredyear_w2wgsal retired_w2wgsal ///
	      se_nppes se_acs se_schedules govt_acs ///
	      wkh wkh_imputed desirable_cz ///
	      (sd) sd_ptotinc = ptotinc sd_logptotinc = logptotinc ///
	      (median) median_ptotinc = ptotinc median_logptotinc = logptotinc, ///
	      by(year) labelformat(#stat#: #sourcelabel#) freq(id)	
	      tempfile clps_all_`age'
	      save `clps_all_`age'' 
	  
	  restore 	  
	  
	}
	
	      
	foreach spec in `specialties' {		
	foreach age in `age_group' {
		
	use `clps_`spec'_`age'', clear
	append using `clps_all_`age'' 
	 if "`spec'" != "cms_spec"   {
	 	replace `spec'_code=0 if missing(`spec'_code) 
	 }
	 if "`spec'" == "cms_spec"  {
	 	replace  `spec'_code="00" if missing(`spec'_code) 
	 }
	replace `spec'_desc="All Physicians" if missing(`spec'_desc) 	
	sort *code 
	rename id freq 
	save "${temp}/physician_clean_collapse_by_`spec'_`age'.dta", replace
	}
	}

	* Add residency duration information
          
 	local specialties cms_spec spec_category nrmp_spec

	local age_group ageall age4055
	
	foreach spec in `specialties' {		
	foreach age in `age_group' {
			
	* Merge to CMS specialty level dataset 
	
	use "${temp}/physician_clean_collapse_by_`spec'_`age'.dta", clear
	merge m:1 `spec'_code using "${temp}/resdur_`spec'.dta", assert(master match) nogen
	save "${intermediate_data}/specialty_characteristics/`spec'_characteristics_`age'.dta", replace
	
	}
	}
